# -*- coding: utf-8 -*-

import logging
import os
import time
import datetime
import redis
import yaml

from scrapy.crawler import CrawlerProcess
from django.core.management.base import BaseCommand, CommandError
from scrapy.utils.log import configure_logging
from scrapy.utils.project import get_project_settings

import config
from socar import settings
from sqlhelper import SqlHelper


# python manage.py clear_bbs_running
class Command(BaseCommand):
    help = 'clear bbs running'

    def add_arguments(self, parser):
        parser.add_argument('-a', action = 'append', dest = 'spargs', default = [],
                            help = 'set spider argument (may be repeated)')

    # 必须实现的方法
    def handle(self, *args, **options):
        os.chdir(settings.BASE_DIR)
        print('start data monitor run time:%s' % str(datetime.datetime.now()))

        with open('%s/conf/data_monitor.yaml' % settings.BASE_DIR, 'r') as f:
            conf = yaml.load(f)

        sta_sql = SqlHelper(db_name = 'autohome_sta', db_config = config.db_168_3306_config)

        for key, val in conf.items():
            if key == 3307:
                sql = SqlHelper(db_name = 'mysql', db_config = config.db_168_3307_config)
                address = 3307
            else:
                sql = SqlHelper(db_name = 'mysql', db_config = config.db_168_3306_config)
                address = 3306

            try:
                dbs = val.get('dbs')
                for db in dbs:
                    db_name = db.get('db')
                    tables = db.get('tables')
                    sql.select_db(db_name = db_name)
                    for table in tables:
                        print('run_table:%s' % table)
                        command = 'SELECT count(*) as count FROM {table}'.format(table = table.get('name'))
                        result = sql.query_one(command, cursor_type = 'dict')

                        command = "SELECT update_time FROM information_schema.TABLES WHERE TABLE_SCHEMA='{db_name}' and table_name='{table_name}';".format(
                            db_name = db_name, table_name = table.get('name'))
                        update = sql.query_one(command, cursor_type = 'dict')

                        d = {
                            'monitor_address': address,
                            'monitor_db_name': db_name,
                            'monitor_table_name': table.get('name'),
                            'monitor_table_id': table.get('id'),
                            'monitor_table_type': table.get('type'),
                            'monitor_count': result.get('count'),
                            'monitor_update_time': update.get('update_time'),
                            'monitor_time': str(datetime.datetime.now()),
                            'monitor_status': 1,
                        }

                        sta_sql.insert_json(d, 'sta_monitor', commit = True)
            except Exception as e:
                print('exception:%s' % e)

        print('end data monitor run time:%s' % str(datetime.datetime.now()))


def arglist_to_dict(arglist):
    """Convert a list of arguments like ['arg1=val1', 'arg2=val2', ...] to a
    dict
    """
    return dict(x.split('=', 1) for x in arglist)
